/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.shop.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.persistence.ResponseResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.shop.entity.Goods;
import com.thinkgem.jeesite.modules.shop.service.GoodsService;

/**
 * 商品信息Controller
 * @author dby
 * @version 2020-06-30
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/goods")
public class GoodsController extends BaseController {

	@Autowired
	private GoodsService goodsService;
	
	@ModelAttribute
	public Goods get(@RequestParam(required=false) String id) {
		Goods entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = goodsService.get(id);
		}
		if (entity == null){
			entity = new Goods();
		}
		return entity;
	}
	
	@RequiresPermissions("shop:goods:view")
	@RequestMapping(value = {"list", ""})
	public String list(Goods goods, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Goods> page = goodsService.findPage(new Page<Goods>(request, response), goods); 
		model.addAttribute("page", page);
		return "modules/shop/goodsList";
	}

	@RequiresPermissions("shop:goods:view")
	@RequestMapping(value = {"list1", ""})
	public String list1(Goods goods, HttpServletRequest request, HttpServletResponse response, Model model) {

		Page<Goods> page = goodsService.findPage(new Page<Goods>(request, response), goods);
		model.addAttribute("page", page);
		return "modules/shop/list1";
	}

	@RequiresPermissions("shop:goods:view")
	@RequestMapping(value = "form")
	public String form(Goods goods, Model model) {
		model.addAttribute("goods", goods);
		return "modules/shop/goodsForm";
	}

	@RequiresPermissions("shop:goods:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public ResponseResult save(Goods goods, Model model, RedirectAttributes redirectAttributes) {
		goodsService.save(goods);
		return ResponseResult.success();
		// {"code":0,"message":''}
	}
	
	@RequiresPermissions("shop:goods:edit")
	@RequestMapping(value = "delete")
	public String delete(Goods goods, RedirectAttributes redirectAttributes) {
		goodsService.delete(goods);
		addMessage(redirectAttributes, "删除商品信息成功");
		return "redirect:"+Global.getAdminPath()+"/shop/goods/?repage";
	}

	/*
	 * @Author Zhang Xingliang
	 * @Description 商品查看页面
	 * @Date 0:27 2020/7/1
	 **/
	@RequiresPermissions("shop:goods:view")
	@RequestMapping(value = "view")
	public String view(Goods goods, Model model) {
		model.addAttribute("goods", goods);
		return "modules/shop/goodsView";
	}
}